<?php
$loader->requireOnce('includes/Utils.class.php');

/**
 * Object Relational Persistence Mapping Class for table: Person
 *
 * @package    com.clear-health.clearhealth
 */
class Person extends ORDataObject {

    /**#@+
     * Fields of table: Person mapped to class members
     */
    var $person_id        			= 	'';
    var $address_id					=	'';
    var $name        				= 	'';
    var $gender        				= 	'';
    var $date_of_birth    			= 	'';
    var $phone_number 				= 	'';
    var $career_id 					= 	'';
    var $email						= 	'';
    var $notes 						=	'';
    var $workingplace_province_id	=	'';
   	var $workingplace_district_id	=	'';
   	var $nationality				=	'';
   	var $pic_path                   =   '';

    /**#@+
     * {@inheritdoc}
     */
    var $_table 			= 	'person';
    var $_key 				= 	'person_id';
    var $_internalName		=	'Person';
    var	$_foreignKeyList	=	array(
    	'nationality'	=>	'country',
    	'career_id'		=>	'career'
    );

    var $address;

    const GENDER_MALE   =   'male';
    const GENDER_FEMALE =   'female';

    /**
     * Setup some basic attributes
     * Shouldn't be called directly by the user, user the factory method on ORDataObject
     */
    public function Person($db = null) {
        parent::ORDataObject($db);
        $this->address  =   Celini::newORDO('Address');
    }

    /**
     * Called by factory with passed in parameters, you can specify the primary_key of Person with this
     */
    function setup($id = 0) {
        $this->set_id($id);
        if ($id > 0) {
            $this->populate();
            $this->address = ORDataObject::factory('Address', $this->address_id);
        }
    }

    public function setData($address_id, $name, $gender, $date_of_birth,
    	$phone_number, $career, $email, $notes, $workingplace_province_id,
    	$workingplace_district_id, $nationality){
    		$data	=	array(
    			'address_id'				=>	$address_id,
    			'name'						=>	$name,
    			'gender'					=>	$gender,
    			'date_of_birth'				=>	$date_of_birth,
    			'phone_number'				=>	$phone_number,
    			'career_id'					=>	$career,
    			'email'						=>	$email,
    			'notes'						=>	$notes,
    			'workingplace_province_id'	=>	$workingplace_province_id,
    			'workingplace_district_id'	=>	$workingplace_district_id,
    			'nationality'				=>	$nationality
    		);
    		$this->populateArray($data);
    	}

    function get_name() {
    	return $this->name;
    }

    function get_email() {
    	return $this->email;
    }

    function set_id($id) {
    	$this->person_id = $id;
    }

    /**
     * Set date of birth, formating it correctly
     */
    public function setDateofBirth($date) {
       $this->date_of_birth=$date;
    }

    /**
     * Returns date of birth as an English date instead of ISO
     *
     * @return string
     * @access protected
     */
    function getDateofBirth() {
        return L10nHelper::renderDateTime($this->date_of_birth, 'medium');
    }

    /**
     * Get the age of the person based on date of birth
     */
    function getAge() {
    	if ($this->date_of_birth == '0000-00-00' || empty($this->date_of_birth)) {
    		return "";
    	}
    	$objDate    =   DateObject::createFromISO($this->date_of_birth);
    	$birthYear  =   $objDate->getYear();
        $curYear    =   date('Y');
        $age       =   (int)$curYear - (int)$birthYear;
    	if ($age <= 0) {
            $age   =    '';
    	}
    	return $age;
    }

    public function getId(){
    	return $this->person_id;
    }

    public function setAddressId($id){
    	$this->address_id=$id;
    }

    public function getAddressId() {
    	return $this->address_id;
    }

	public function getName(){
       return $this->name;
	}

   	public function getGender(){
        return $this->gender;
   	}

   	public function getCareer(){
   		return $this->career_id;
   	}

   	public function getCareerName($culture = 'vi-VN'){
   		$obj	=&	Celini::newORDO('Career',$this->career_id);
   		return $obj->getLocalizedText('name');
   	}

   	public function getAddress(){
		return $this->address->getAddress();
   	}

   	public function getPhoneNumber(){
   		return $this->phone_number;
   	}

	public function getStreet() {
		return $this->address->getStreet();
	}

	public function getProvinceId() {
		return $this->address->getProvinceId();
	}

	public function getDistrictId() {
		return $this->address->getDistrictId();
	}

	public function getWardName() {
		return $this->address->getWardName();
	}

	public function getIdsByName($name){
   		$ds =& new Datasource_sql();
        $sql = array(
            'cols'		=> "person_id",
            'from'		=> "$this->_table",
        	'where'		=> "name='$name'",
            'orderby'	=> 'person_id'
            );
        $cols = array('person_id' => 'ID');
        $ds->setup($this->_db,$sql,$cols);
        return $ds->toArray('person_id');
   }

   	public function getNameById($personId){
		$ds = new Datasource_sql();
		$sql = array(
			'cols' 		=> "name",
			'from' 		=> "person",
			'where' 	=> "person_id = '$personId'"
			);

		$ds->setup($this->_db,$sql);
		$result = $ds->toArray();
		if (!empty($result)) return $result[0];
		return $result;
   	}

   	public function getWorkingPlace(){
   		$province	=	&ORDataObject::factory('Province');
   		$district	=	&ORDataObject::factory('District');
   		$province->setup($this->workingplace_province_id);
   		$district->setup($this->workingplace_district_id);
   		return L10nHelper::getAddress($province->getLocalizedText('name'), $district->getLocalizedText('name'));
   	}

   	public function getWorkingProvinceId(){
   		return $this->workingplace_province_id;
   	}

 	public function getWorkingDistrictId(){
   		return $this->workingplace_district_id;
   	}

   	public function getNationality(){
   		return $this->nationality;
   	}

   	public function getNationalityObject(){
   		$country	=	Celini::newORDO('Country');
   		$country->setup($this->nationality);
   		return $country;
   	}

   	public function getCareerObject(){
   		$career	=	Celini::newORDO('Career');
   		$career->setup($this->career_id);
   		return $career;
   	}

   	public function getObjectById($personId) {
   		$sql = "select * from $this->_table where $this->_key=$personId";
   		$res = $this->_execute($sql);

   		if ( $res && !$res->EOF) {
   			return Celini::newORDO('Person', $res->fields[$this->_key]);

   		} else {
   			return false;

   		}

   	}

   	public function deletePerson($personId) {
   		$objPerson	=	Celini::newORDO('Person', $personId);
   		$sql = "delete from $this->_table where $this->_key=$personId";
   		$res	=	$this->_execute($sql);
   		if ($res === false) {
   			return $res;
   		}
		$addressId	=	$objPerson->getAddressId();
		$sql	=	"DELETE FROM address WHERE address_id = '$addressId'";
    	$res	=	$this->_execute($sql);

   		return $res;
   	}

}
?>
